package net.roseparadise.util;


import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import net.roseparadise.config.PropertiesConfig;
import net.roseparadise.exception.ParameterLengthException;
import net.roseparadise.model.Member;
import net.roseparadise.vo.UserVO;

public class VOUtils {
	public static String getCurrentDateString(){
		SimpleDateFormat sdf = new SimpleDateFormat();
		sdf.applyPattern("yyyymmddhhMMss");
		return sdf.format(new Date());
	}
	
	public static UserVO getUserVO(Member member){
		UserVO user = new UserVO();
		user.setId(member.getId());
		user.setName(member.getName());
		user.setEmail(member.getEmail());
		user.setMsn(member.getMsn());
		user.setQq(member.getQq());
		user.setTelephone(member.getTelephone());
		user.setRole(member.getRole());
		user.setCreatedate(member.getCreatedate());
		return user;
	}
	
	public static String validateParameter(HttpServletRequest request,String pramName,String defaultValue,int minLength,int maxLength)throws ParameterLengthException
	{
		String param = (String)request.getParameter(pramName);
		if( param == null ){
			param = defaultValue;
		}
		if(param.length()<minLength || param.length()>maxLength ){
			param = defaultValue;
			throw new ParameterLengthException();
		}
		return param;
	}
	
	public static String validateParameter(HttpServletRequest request,String pramName,String defaultValue,int minLength)throws ParameterLengthException
	{
		String param = (String)request.getParameter(pramName);
		if( param == null ){
			param = defaultValue;
		}
		if(param.length()<minLength ){
			param = defaultValue;
			throw new ParameterLengthException();
		}
		return param;
	}
	
	public static String validateParameter(HttpServletRequest request,String pramName,String defaultValue)
	{
		String param = (String)request.getParameter(pramName);
		if( param == null ){
			param = defaultValue;
		}		
		return param;
	}
	public static boolean checkCurrentUserRights(HttpServletRequest request,String rightName)
	{
		boolean result = false;
		try {
			UserVO uservo = (UserVO)request.getSession().getAttribute("currentUser");
			
			if( uservo != null ){
				result  = uservo.getRole() > (PropertiesConfig.getInstance().getInt(rightName)-1);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}		
		return result ;
	}
}
